local GameLunchActors = class("GameLunchActors")
local Vector3 = require("utils.Vector3")
local Monster = require("gameLogic.actor.Monster")
local GameUtils = require("utils.GameUtils")
local ObjectPoolConst = require("const.ObjectPoolConst")

local MAX_MONSTER = 7

function GameLunchActors:ctor(sceneId, sceneObject)
    self._sceneId = sceneId
    self._sceneObject = sceneObject
    self._otherPlayers = {}
    self._recyclActos = {}
    self._monsterMaps = {}
    self._otherPlayerPool =
        G_ObjectPoolManager:addObjetcPool(ObjectPoolConst.OTHERPLAYER_KEY, require("gameLogic.actor.OtherPlayer"))
end

function GameLunchActors:clearOtherPlayer()
    for _, player in pairs(self._otherPlayers) do
        player:remove()
    end
    self._otherPlayers = {}
end

function GameLunchActors:clearMonsters()
    for _, monster in ipairs(self._monsterMaps) do
        monster:remove()
    end
    self._monsterMaps = {}
end

function GameLunchActors:addMonster(monsterId, pos, isFight)
    local monster = self._monsterMaps[monsterId]
    if not monster then
        monster =
            G_ObjectPoolManager:getObject(
            ObjectPoolConst.MONSTER_KEY .. monsterId,
            Monster,
            monsterId,
            self._sceneObject
        )
        monster:setPosition(pos)
        monster:setScale(Vector3.new(1.5, 1.5, 1.5))
        monster:setFight(isFight)
        self._monsterMaps[monsterId] = monster
    end
end

function GameLunchActors:monsterTrack(monsterId, trackId)
    local monster = self._monsterMaps[monsterId]
    local player = self._otherPlayers[trackId]
    if monster and player then
        monster:setTrackTarget(player.transform)
    end
end

function GameLunchActors:monsterLeaveTrack(monsterId)
    local monster = self._monsterMaps[monsterId]
    if monster then
        monster:leaveTrack()
    end
end

function GameLunchActors:setMonsterFight(monsterId, fight)
    local monster = self._monsterMaps[monsterId]
    if monster then
        monster:monsterFight()
    end
end

function GameLunchActors:setMonsterStatus(monsterId, status, topos)
    local monster = self._monsterMaps[monsterId]
    if monster then
        printDebug("GameLunchActors:setMonsterStatus " .. monsterId .. " " .. status)
        if status == 1 then
            monster:setFight(false)
        elseif status == 2 then
            monster:setFight(true)
            monster:setTrackTarget(Vector3.new(topos.x, topos.y, topos.z))
        elseif status == 3 then
            monster:remove()
            self._monsterMaps[monsterId] = nil
        end
    end
end

function GameLunchActors:removeMonster(monsterId)
    local monster = self._monsterMaps[monsterId]
    if monster then
        monster:remove()
    end
    self._monsterMaps[monsterId] = nil
end

function GameLunchActors:updateOtherPlayer(param)
    local player = self._otherPlayers[param.userId]
    if not player then
        player = self._otherPlayerPool:getObject(self._sceneObject)
        local name = G_UserData:getBase():getOtherPlayerName(param.userId)
        player:setName(name)
        self._otherPlayers[param.userId] = player
    end
    if param.pos then
        player:setPosition(param.pos)
    end
    if param.forward then
        player:setForward(param.forward)
    end
    if param.velocity then
        player:setVelocity(param.velocity)
    end
end

function GameLunchActors:moveOtherPlayers(dt)
    for _, player in pairs(self._otherPlayers) do
        player:updateDt(dt)
    end
end

function GameLunchActors:removePlayer(userId)
    printDebug("GameLunchActors:removePlayer " .. userId)
    local player = self._otherPlayers[userId]
    if player then
        player:remove()
    end
    self._otherPlayers[userId] = nil
end

return GameLunchActors
